package com.zzxz.practicaltrainingsys.service.impl;

import com.zzxz.practicaltrainingsys.vo.ViewMember;
import com.zzxz.practicaltrainingsys.entity.Member;
import com.zzxz.practicaltrainingsys.entity.Student;
import com.zzxz.practicaltrainingsys.mapper.MemberMapper;
import com.zzxz.practicaltrainingsys.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.zzxz.practicaltrainingsys.component.WebSocketServiceHelper.teamMapper;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {


    @Autowired
    private MemberMapper memberMapper;

    @Override
    public int insertMember(Member member) {
        System.out.println(member.toString());
        return  memberMapper.insertMember(member);
    }

    @Override
    public List<String> getStudentIdsByGroupId(Integer team_id) {
        return memberMapper.getStudentIdsByTeamId(team_id);
    }

    @Override
    public List<Student> getStudents(Integer team_id) {
        return memberMapper.getStudents(team_id);
    }

    @Override
    public int updateStudentScore(Member member) {
        return memberMapper.updateStudentScore(member);
    }

    @Override
    public Integer getScoreById(String student_id) {
        return memberMapper.getScoreById(student_id);
    }

    @Override
    public int checkMemberExist(Integer team_id, Integer student_id) {
        return memberMapper.checkMemberExist(team_id, student_id);
    }

    @Override
    public List<Member> findByStudentId(Integer student_id) {
        return memberMapper.findByStudentId(student_id);
    }

    @Override
    public List<Member> listByTeamId(Integer team_id) {
        return memberMapper.listByTeamId(team_id);
    }

    @Override
    public boolean checkStudentInProjectTeam(Integer student_id, Integer project_id) {
        // 查找该项目下所有 team_id
        List<Integer> teamIds = teamMapper.getTeamIdsByProjectId(project_id);
        if (teamIds == null || teamIds.isEmpty()) return false;
        // 查找 member 表中是否有 student_id 属于这些 team_id
        return memberMapper.countByStudentAndTeamIds(student_id, teamIds) > 0;
    }

    @Override
    public List<ViewMember> listMemberInfoByTeamId(Integer team_id) {
        return memberMapper.listMemberInfoByTeamId(team_id);
    }
    @Override
    public com.zzxz.practicaltrainingsys.vo.ProjectTeamInfo getProjectTeamInfo(Integer student_id, Integer project_id) {
        return memberMapper.selectProjectTeamInfo(student_id, project_id);
    }

}
